<template>
  <div>
    <h1>角色管理</h1>
    <el-form :inline="true" :model="queryRole" class="demo-form-inline">
      <el-form-item label="角色">
        <el-input v-model="queryRole.role" placeholder="角色"></el-input>
      </el-form-item>
      <el-form-item label="权限字符">
        <el-input v-model="queryRole.username" placeholder="权限字符"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="queryRole.status" placeholder="状态">
          <el-option label="激活" value="1"></el-option>
          <el-option label="未激活" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-col :span="11">
          <el-date-picker type="date" placeholder="选择日期" value-format="yyyy-MM-dd HH:mm:ss" v-model="queryRole.t1" style="width: 100%;"></el-date-picker>
        </el-col>
        <el-col class="line" :span="2">-</el-col>
        <el-col :span="11">
          <el-date-picker type="date" placeholder="选择日期" value-format="yyyy-MM-dd HH:mm:ss" v-model="queryRole.t2" style="width: 100%;"></el-date-picker>
        </el-col>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="list">查询</el-button><br>
      </el-form-item>
    </el-form>
    <el-button @click="openAdd" type="primary">添加</el-button>

    <el-table
        :data="tableData"
        style="width: 100%">
      <el-table-column
          prop="id"
          label="id"
          width="180">
      </el-table-column>
      <el-table-column
          prop="role"
          label="角色名称"
          width="180">
      </el-table-column>
      <el-table-column
          prop="username"
          label="权限字符">
      </el-table-column>
      <el-table-column
          label="状态">
        <template v-slot="s">
          <el-switch
              v-model="s.row.status"
              active-color="#13ce66"
              inactive-color="#ff4949">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
          prop="createTime"
          label="创建时间">
      </el-table-column>
      <el-table-column>
        <template v-slot="s">
          <el-button @click="del(s.row.id)" type="danger">删除</el-button>
          <el-button @click="openupdate(s.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @current-change="handleCurrentChange"
        :page-size="size"
        layout="total, prev, pager, next"
        :total="total">
    </el-pagination>

    <el-dialog title="修改" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="角色名称" :label-width="formLabelWidth">
          <el-input v-model="form.role" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="权限标识符" :label-width="formLabelWidth">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-select v-model="form.status" placeholder="状态">
            <el-option label="激活" value="1"></el-option>
            <el-option label="未激活" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间" :label-width="formLabelWidth">
          <el-date-picker type="date" placeholder="选择日期" value-format="yyyy-MM-dd HH:mm:ss" v-model="form.createTime" style="width: 100%;"></el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="update">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="添加" :visible.sync="dialogFormVisible1">
      <el-form :model="form1">
        <el-form-item label="角色名称" :label-width="formLabelWidth1">
          <el-input v-model="form1.role" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="权限标识符" :label-width="formLabelWidth1">
          <el-input v-model="form1.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth1">
          <el-select v-model="form1.status" placeholder="状态">
            <el-option label="激活" value="1"></el-option>
            <el-option label="未激活" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间" :label-width="formLabelWidth1">
          <el-date-picker type="date" placeholder="选择日期" value-format="yyyy-MM-dd HH:mm:ss" v-model="form1.createTime" style="width: 100%;"></el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

export default {
  name: "RoleView",
  data() {
    return {
      tableData:[],
      current:1,
      size:2,
      total:0,
      queryRole:{},
      dialogFormVisible:false,
      form:{},
      formLabelWidth:'120px',
      formLabelWidth1:'120px',
      dialogFormVisible1:false,
      form1:{}
    }
  }, methods: {
    handleCurrentChange(current){
      this.current=current;
      this.list();
    },
    list(){
      this.axios.post("/role/list?current="+this.current+"&size="+this.size,this.queryRole).then(res =>{
        this.tableData = res.data.data.list;
        this.total = res.data.data.total;
      })
    },
    del(id){
      this.axios.post("/role/del?id="+id).then(() =>{
        this.$message.success("删除成功");
        this.list();
      })
    },
    openupdate(row){
      this.form=row;
      this.dialogFormVisible=true;
    },
    update(){
      this.axios.post("/role/upodate",this.form).then(() =>{
        this.$message.success("修改成功");
        this.dialogFormVisible=false;
        this.list();
      })
    },
    openAdd(){
      this.dialogFormVisible1=true;
    },

  }, created() {
    this.list();
  }
}
</script>

<style scoped>

</style>
